/*
 * ©2023 JOY-HUBS.COM
 */

package com.joyhubs.base.advice;

import org.springframework.http.MediaType;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;

import com.joyhubs.base.entity.ContentBody;

/**
 * ContentBody wrapper http converter
 */
public class ContentBodyMessageConverter extends MappingJackson2HttpMessageConverter {
    
    @Override
    public boolean canWrite(Class<?> clazz, MediaType mediaType) {
        return ContentBody.class == clazz || (String.class == clazz && supportMedia(mediaType));
    }
    
    /**
     * supported media type for ContentBody wrapper response
     *
     * @param mediaType
     * @return
     */
    private boolean supportMedia(MediaType mediaType) {
        return mediaType == null || MediaType.APPLICATION_JSON.includes(mediaType);
    }
    
}
